//
//  MJPlayerTool.m
//  MJAVFoundationDemo
//
//  Created by apple on 16/9/12.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "MJPlayerTool.h"

@implementation MJPlayerTool
#pragma mark--获取歌曲图片文件
+(NSMutableArray *)getDataWithType:(NSString *)type andSuffixName:(NSString *)name
{
    NSArray *arr=[[NSBundle mainBundle]pathsForResourcesOfType:type inDirectory:nil];
    NSMutableArray *selectedArr=[NSMutableArray array];
    for (NSString * imageName in arr) {
        if ([imageName containsString:name]) {
            [selectedArr addObject:[imageName lastPathComponent]];
        }
    }
    return selectedArr;
   
}
#pragma mark--歌名

+(NSArray *)getSongArr
{
   
   return @[@"I want it that way",@"泡沫",@"最佳损友",@"简单爱",@"一千年以后",@"小苹果",@"瓦解",@"传奇",@"心碎了无痕"];
}
#pragma mark--获取歌者

+(NSArray *)getSingerArr
{
     return @[@"Backstreet Boys",@"G.E.M.邓紫棋",@"陈奕迅",@"周杰伦",@"林俊杰",@"筷子兄弟",@"南拳妈妈",@"王菲",@"张学友"];
}
#pragma mark--获取歌曲图片icon
+(NSMutableArray *)getBgImgArr
{
    NSMutableArray *bgImageArr=[NSMutableArray array];
    NSMutableArray *allArr=[self getDataWithType:@"jpg" andSuffixName:@"@2x"];
    for (NSString *imgName in allArr) {
        if (![imgName hasSuffix:@"_icon@2x.jpg"]) {
            [bgImageArr addObject:imgName];
        }
    }
    return bgImageArr;
}
#pragma mark--解析歌词时间
+(void)paserLrcWithMusicPath:(NSString *)lrcFile finished:(void(^)(NSMutableArray *musicTime,NSMutableArray *content))block
{
    //获取歌词路径
    NSString *lrcPath=[[NSBundle mainBundle]pathForResource:lrcFile ofType:nil];
    //如果路径存在
    if ([lrcPath length]) {
        //歌词编码
        NSString *lyc = [NSString stringWithContentsOfFile:lrcPath encoding:NSUTF8StringEncoding error:nil];
        //创建数组分别装歌词和时间
        NSMutableArray *timeArr=[NSMutableArray array];
        NSMutableArray *contentArr=[NSMutableArray array];
        //将每一行的歌词分开装起来
        NSArray *arr=[lyc componentsSeparatedByString:@"\n"];
        for (NSString *item in arr) {
            //如果每一句歌词获取成功
            if ([item length]) {
                //从[开始
                NSRange startrange = [item rangeOfString:@"["];
                //从]结束
                NSRange stoprange = [item rangeOfString:@"]"];
                //根据范围截取时间
                NSString *content = [item substringWithRange:NSMakeRange(startrange.location+1, stoprange.location-startrange.location-1)];
                //如果截取成功
                if ([content length] == 8) {
                    //分别截取分:秒:毫秒
                    NSString *minute = [content substringWithRange:NSMakeRange(0, 2)];
                    NSString *second = [content substringWithRange:NSMakeRange(3, 5)];
                    //NSString *mm = [content substringWithRange:NSMakeRange(6, 2)];
                    //拼接字符串
                    //NSString *time = [NSString stringWithFormat:@"%@:%@",minute,second];
                    //这里我只要一个秒数
                    NSInteger time=minute.integerValue*60+second.floatValue;
                    NSString *lyric = [item substringFromIndex:10];
                    //装入数组
                    [timeArr addObject:@(time)];
                    [contentArr addObject:lyric];
                    
                }
            }
        }
        //回调block传参
        block(timeArr,contentArr);
    }
}
@end
